package com.ruoyi.web.controller.jjm;

import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.jjm.domain.Supplier;
import com.ruoyi.jjm.service.ISupplierService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 供应商Controller
 * 
 * @author tsr
 * @date 2020-07-15
 */
@Controller
@RequestMapping("/jjm/supplier")
public class SupplierController extends BaseController
{
    private String prefix = "jjm/supplier";

    private static final long SUPPLIER_ROLE_ID = 104L;

    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private SysPasswordService passwordService;

    @RequiresPermissions("jjm:supplier:view")
    @GetMapping()
    public String supplier()
    {
        return prefix + "/supplier";
    }

    /**
     * 查询供应商列表
     */
    @RequiresPermissions("jjm:supplier:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Supplier supplier)
    {
        supplier.setAreaId(ShiroUtils.getSysUser().getDeptId());
        startPage();
        List<Supplier> list = supplierService.selectSupplierList(supplier);
        return getDataTable(list);
    }

    /**
     * 导出供应商列表
     */
    @RequiresPermissions("jjm:supplier:export")
    @Log(title = "供应商", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Supplier supplier)
    {
        List<Supplier> list = supplierService.selectSupplierList(supplier);
        ExcelUtil<Supplier> util = new ExcelUtil<Supplier>(Supplier.class);
        return util.exportExcel(list, "supplier");
    }

    /**
     * 新增供应商
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存供应商
     */
    @RequiresPermissions("jjm:supplier:add")
    @Log(title = "供应商", businessType = BusinessType.INSERT)
    @Transactional
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Supplier supplier)
    {
        supplier.setAreaId(ShiroUtils.getSysUser().getDeptId());
        if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(supplierService.checkNameUnique(supplier)))
        {
            return error("新增供应商'" + supplier.getName() + "'失败，供应商已存在");
        }
        else if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(supplier.getLoginName())))
        {
            return error("新增供应商'" + supplier.getName() + "'失败，登录账号已存在");
        }
//        else if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(supplierService.checkPhoneUnique(supplier)))
//        {
//            return error("新增供应商'" + supplier.getName() + "'失败，手机号码已存在");
//        }
        SysUser user = new SysUser();
        user.setLoginName(supplier.getLoginName());
        user.setOriginalPwd(supplier.getLoginPassword());
        user.setUserName(supplier.getName());
        user.setPhonenumber(supplier.getPhoneNumber());
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getOriginalPwd(), user.getSalt()));
        user.setCreateBy(ShiroUtils.getLoginName());
        user.setRoleIds(new Long[]{SUPPLIER_ROLE_ID});
//        user.setDeptId(ShiroUtils.getSysUser().getDeptId());
        userService.insertUser(user);
        supplier.setCreateBy(ShiroUtils.getLoginName());
        supplier.setUserId(user.getUserId());
        return toAjax(supplierService.insertSupplier(supplier));
    }

    /**
     * 修改供应商
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Supplier supplier = supplierService.selectSupplierById(id);
        mmap.put("supplier", supplier);
        return prefix + "/edit";
    }

    /**
     * 修改保存供应商
     */
    @RequiresPermissions("jjm:supplier:edit")
    @Log(title = "供应商", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Supplier supplier)
    {
        SysUser user = new SysUser();
        user.setUserId(supplier.getUser().getUserId());
        user.setLoginName(supplier.getLoginName());
        if (UserConstants.USER_PHONE_NOT_UNIQUE.equals(supplierService.checkNameUnique(supplier)))
        {
            return error("修改供应商'" + supplier.getName() + "'失败，供应商已存在");
        }
        else if (UserConstants.USER_NAME_NOT_UNIQUE.equals(userService.checkLoginNameUnique(user)))
        {
            return error("修改供应商'" + supplier.getName() + "'失败，登录账号已存在");
        }
        user.setOriginalPwd(supplier.getLoginPassword());
        user.setUserName(supplier.getName());
        user.setPhonenumber(supplier.getPhoneNumber());
        user.setSalt(ShiroUtils.randomSalt());
        user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getOriginalPwd(), user.getSalt()));
        user.setCreateBy(ShiroUtils.getLoginName());
        user.setRoleIds(new Long[]{SUPPLIER_ROLE_ID});
//        user.setDeptId(ShiroUtils.getSysUser().getDeptId());
        userService.updateUser(user);
        supplier.setCreateBy(ShiroUtils.getLoginName());
        supplier.setUserId(user.getUserId());
        return toAjax(supplierService.updateSupplier(supplier));
    }

    /**
     * 删除供应商
     */
    @RequiresPermissions("jjm:supplier:remove")
    @Log(title = "供应商", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(supplierService.deleteSupplierByIds(ids));
    }


    /**
     * 校验供应商名称
     */
    @PostMapping("/checkNameUnique")
    @ResponseBody
    public String checkNameUnique(Supplier supplier)
    {
        supplier.setAreaId(ShiroUtils.getSysUser().getDeptId());
        return supplierService.checkNameUnique(supplier);
    }
 /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(Supplier supplier)
    {
        return supplierService.checkPhoneUnique(supplier);
    }
}
